package com.keon.jianbao.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.keon.jianbao.R;

import java.util.List;

/**
 * Created by keon on 2015/8/24.
 */
public class BaseListNoPageAdapter<T> extends BaseAdapter {
    public static final int STATE_NO_DATA = 0;
    public static final int STATE_FINISH = 1;
    public static final int STATE_OTHER = 2;

    protected int state = STATE_FINISH;
    protected List<T> mDatas;
    private LinearLayout mFooterView;
    private LayoutInflater mInflater;

    protected int _loadFinishText;
    protected int _noDateText;

    public BaseListNoPageAdapter(List<T> mDatas) {
        this.mDatas = mDatas;
        if (getDataSize() == 0) {
            state = STATE_NO_DATA;
        }
        _loadFinishText = R.string.loading_no_more;
        _noDateText = R.string.error_view_no_data;
    }

    protected LayoutInflater getLayoutInflater(Context context) {
        if (mInflater == null) {
            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        return mInflater;
    }

    public int getState() {
        return this.state;
    }

    @Override
    public int getCount() {
        switch (state) {
            case STATE_FINISH:
                return getDataSize() + 1;
            case STATE_NO_DATA:
                return 1;
        }
        return getDataSize();
    }

    public int getDataSize() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        if (mDatas.size() > position) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (position == getCount() - 1) {// 最后一条
            // if (position < _data.size()) {
            // position = getCount() - 2; // footview
            // }
            this.mFooterView = (LinearLayout) LayoutInflater.from(
                    parent.getContext()).inflate(R.layout.list_cell_footer,
                    null);
            ProgressBar progress = (ProgressBar) mFooterView
                    .findViewById(R.id.progressbar);
            TextView text = (TextView) mFooterView.findViewById(R.id.text);
            switch (getState()) {
                case STATE_FINISH:
                    mFooterView.setVisibility(View.VISIBLE);
                    progress.setVisibility(View.GONE);
                    text.setVisibility(View.VISIBLE);
                    text.setText(_loadFinishText);
                    break;
                case STATE_NO_DATA:
                    progress.setVisibility(View.GONE);
                    mFooterView.setVisibility(View.VISIBLE);
                    text.setText(_noDateText);
                    break;
                default:
                    progress.setVisibility(View.GONE);
                    mFooterView.setVisibility(View.GONE);
                    text.setVisibility(View.GONE);
                    break;
            }
            return mFooterView;
        }
        if (position < 0) {
            position = 0; // 若列表没有数据，是没有footview/headview的
        }
        return getRealView(position, view, parent);
    }

    protected View getRealView(int position, View convertView, ViewGroup parent) {
        return null;
    }
}
